﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace OneCaps.Core.Extensions
{
    public static class StringExtensions
    {
        // 1. 空值检查
        public static bool IsNullOrEmpty(this string value)
            => string.IsNullOrEmpty(value);

        public static bool IsNullOrWhiteSpace(this string value)
            => string.IsNullOrWhiteSpace(value);

        // 2. 格式化扩展
        public static string FormatWith(this string format, params object[] args)
            => string.Format(format, args);

        // 3. 安全转换
        public static int ToInt(this string value, int defaultValue = 0)
            => int.TryParse(value, out int result) ? result : defaultValue;

        public static DateTime ToDateTime(this string value, DateTime? defaultValue = null)
            => DateTime.TryParse(value, out DateTime result) ? result : defaultValue ?? DateTime.MinValue;

        // 4. 加密相关
        public static string ToMd5(this string input)
        {
            using var md5 = MD5.Create();
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);
            return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
        }

        public static string ToBase64(this string plainText)
            => Convert.ToBase64String(Encoding.UTF8.GetBytes(plainText));

        // 5. 字符串处理
        public static string Truncate(this string value, int maxLength, string suffix = "...")
        {
            if (string.IsNullOrEmpty(value)) return value;
            return value.Length <= maxLength
                ? value
                : value.Substring(0, maxLength) + suffix;
        }

        public static string RemoveWhitespace(this string input)
            => new string(input.Where(c => !char.IsWhiteSpace(c)).ToArray());

        // 6. 集合操作
        public static string Join(this IEnumerable<string> values, string separator)
            => string.Join(separator, values);

        // 7. 正则扩展
        public static bool IsMatch(this string input, string pattern)
            => Regex.IsMatch(input, pattern);

        public static string RegexReplace(this string input, string pattern, string replacement)
            => Regex.Replace(input, pattern, replacement);

        // 8. 编码转换
        public static string ToUrlEncode(this string value)
            => WebUtility.UrlEncode(value);

        public static string ToHtmlEncode(this string value)
            => WebUtility.HtmlEncode(value);

        // 9. 路径处理 (特别适合WPF文件操作)
        public static string EnsureTrailingSlash(this string path)
            => path.EndsWith("/") || path.EndsWith("\\") ? path : path + Path.DirectorySeparatorChar;

        public static string ToSafeFileName(this string name, char replaceChar = '_')
        {
            var invalidChars = Path.GetInvalidFileNameChars();
            return new string(name.Select(c => invalidChars.Contains(c) ? replaceChar : c).ToArray());
        }

        // 10. 值比较扩展
        public static bool EqualsIgnoreCase(this string source, string target)
            => string.Equals(source, target, StringComparison.OrdinalIgnoreCase);

        public static bool ContainsIgnoreCase(this string source, string value)
            => source?.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0;
    }
}
